<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Document</title>
    <script src="./node_modules/axios/dist/axios.min.js"></script>

    <style>
        body{
            margin: 0;
            padding: 30px;
        }
    </style>
</head>
<body>
    <h1>修改用户</h1>
    用户名: <input type="text" class="username"><br>
    密码: <input type="text" class="password"><br>
    <button>提交</button>

    <script>
        // 获取从首页index.html携带过来的id的值
        // 方式一：手动解析
        console.log(location.search)
        console.log(location.search.replace('?','').split('='))
        console.log(location.search.replace('?','').split('=')[1])
        // const id = location.search.replace('?','').split('=')[1]
        // console.log(id)

        // 方式二(URLsearchparams对象)
        const id = new URLSearchParams(location.search).get('id')
        console.log(id)
        // 发送axios的get请求
        axios.get(`http://localhost:3000/users/${id}`).then(res=>{

            // 获取回显数据
            const user = res.data
            document.querySelector('.username').value= user.username
            document.querySelector('.password').value= user.password

        })
        // 发送put请求
        document.querySelector('button').addEventListener('click',function(){
            // 获取用户名和密码
            const username = document.querySelector('.username').value
            const password = document.querySelector('.password').value

            // 发送put请求
            axios({
                method:'put',
                url:`http://localhost:3000/users/${id}`,
                data:{username:username,password:password}
            }).then(res=>{
                location.href='./index.html'
            })
        })
    </script>
</body>
</html>